<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>股票分析系统</title>
    <link href="https://cdn.jsdelivr.net/npm/tailwindcss@2.2.19/dist/tailwind.min.css" rel="stylesheet">
</head>
<body class="bg-gray-100">
    <div class="container mx-auto px-4 py-8">
        <h1 class="text-3xl font-bold mb-8 text-center">股票分析系统</h1>
        
        <div class="grid grid-cols-1 md:grid-cols-2 gap-8">
            <!-- 单只股票分析 -->
            <div class="bg-white p-6 rounded-lg shadow-md">
                <h2 class="text-xl font-semibold mb-4">单只股票分析</h2>
                <div class="mb-4">
                    <input type="text" id="singleStock" 
                           class="w-full p-2 border rounded"
                           placeholder="输入股票代码（如：600000）">
                </div>
                <button onclick="analyzeSingleStock()"
                        class="w-full bg-blue-600 text-white py-2 rounded hover:bg-blue-700">
                    分析
                </button>
            </div>

            <!-- 批量分析 -->
            <div class="bg-white p-6 rounded-lg shadow-md">
                <h2 class="text-xl font-semibold mb-4">批量股票分析</h2>
                <div class="mb-4">
                    <textarea id="batchStocks" 
                              class="w-full p-2 border rounded h-32"
                              placeholder="输入多个股票代码，每行一个"></textarea>
                </div>
                <button onclick="analyzeBatchStocks()"
                        class="w-full bg-blue-600 text-white py-2 rounded hover:bg-blue-700">
                    批量分析
                </button>
            </div>
        </div>

        <!-- 结果展示 -->
        <div id="results" class="mt-8 bg-white p-6 rounded-lg shadow-md">
            <h2 class="text-xl font-semibold mb-4">分析结果</h2>
            <div id="resultContent" class="prose"></div>
        </div>
    </div>

    <script>
        async function analyzeSingleStock() {
            const stockCode = document.getElementById('singleStock').value.trim();
            if (!stockCode) {
                alert('请输入股票代码');
                return;
            }

            try {
                const response = await fetch('/api/analyze', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify({ stock_code: stockCode })
                });

                const result = await response.json();
                if (response.ok) {
                    displayResults([result]);
                } else {
                    alert(result.error || '分析失败');
                }
            } catch (error) {
                alert('请求失败: ' + error.message);
            }
        }

        async function analyzeBatchStocks() {
            const stocksText = document.getElementById('batchStocks').value.trim();
            if (!stocksText) {
                alert('请输入股票代码');
                return;
            }

            const stockList = stocksText.split('\n').map(s => s.trim()).filter(s => s);

            try {
                const response = await fetch('/api/batch-analyze', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify({ stock_list: stockList })
                });

                const results = await response.json();
                if (response.ok) {
                    displayResults(results);
                } else {
                    alert(results.error || '分析失败');
                }
            } catch (error) {
                alert('请求失败: ' + error.message);
            }
        }

        function displayResults(results) {
            const resultContent = document.getElementById('resultContent');
            let html = '';

            results.forEach(result => {
                html += `
                    <div class="mb-8 p-4 border rounded">
                        <h3 class="text-lg font-semibold mb-2">股票代码: ${result.stock_code}</h3>
                        <div class="grid grid-cols-2 gap-4">
                            <div>
                                <p><strong>分析日期:</strong> ${result.analysis_date}</p>
                                <p><strong>当前价格:</strong> ¥${result.price.toFixed(2)}</p>
                                <p><strong>价格变动:</strong> ${result.price_change.toFixed(2)}%</p>
                            </div>
                            <div>
                                <p><strong>综合评分:</strong> ${result.score}分</p>
                                <p><strong>投资建议:</strong> ${result.recommendation}</p>
                                <p><strong>RSI指标:</strong> ${result.rsi.toFixed(2)}</p>
                            </div>
                        </div>
                        <div class="mt-4">
                            <h4 class="font-semibold mb-2">AI分析:</h4>
                            <p class="whitespace-pre-line">${result.ai_analysis}</p>
                        </div>
                    </div>
                `;
            });

            resultContent.innerHTML = html;
        }
    </script>
</body>
</html>